package com.imay.live.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.imay.base.activity.BaseActivity;
import com.imay.live.adapter.HelpAdapter;
import com.imay.live.model.HelpModel;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.utils.RecyclerViewUtil;
import com.imay.live.R;

import java.util.List;

import static com.imay.live.proto.ResultCode.CodeSuccess;

/**
 * create by nick
 * 我的->设置->条款和公约
 */
public class ProvisionActivity extends BaseActivity implements ProtoCallBack, BaseQuickAdapter.OnItemClickListener {

    private RecyclerView rcv_provision;

    public static void start(Context context) {
        Intent starter = new Intent(context, ProvisionActivity.class);
//        starter.putExtra();
        context.startActivity(starter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_provision;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setTitle(getString(R.string.setting_provision));
        rcv_provision = $(R.id.rcv_provision);
        RecyclerViewUtil.setLinearLayoutManager(this,rcv_provision);
        loadData();

    }

    private void loadData(){
         ProtoRequestHelper.task(ProtoHttpFactory.getAgreements()).setTag(this.hashCode()).request(this);

    }

    @Override
    public void onRequestSuccess(String command, String data) {
        Result<List<HelpModel.HelpItemModel>> result = JSON.parseObject(data,new TypeReference<Result<List<HelpModel.HelpItemModel>>>(){}.getType());
        if (result.result==CodeSuccess){
            HelpAdapter adapter = new HelpAdapter(result.data);
            adapter.setOnItemClickListener(this);
            rcv_provision.setAdapter(adapter);
        }

    }

    @Override
    public void onRequestError(String command, int code, String message) {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        HelpModel.HelpItemModel model = (HelpModel.HelpItemModel) adapter.getItem(position);
        WebViewActivity.startActivity(this,model.getUrl());


    }
}
